<?php
/**
 * desc Created by wang.
 * User: wang
 * motto: 是时候为小时候吹过的牛而努力了!
 * Date: 2020/3/23 16:38
 */

namespace app\common\model\mysql;


use think\db\Where;
use think\Model;

class Manager extends Model
{
    protected $name = 'admin_user';
    protected $createTime = 'last_login_time';
    protected $autoWriteTimestamp = true;


    /**
     * 功能说明 通过ID更新数据
     * @Roleor: wang
     * @Date: 2020/3/24 18:17
     * @param $id
     * @param $data
     * @return bool
     */
    public function edit($data)
    {
        $where[] = ['id', '=', $data['id']];
        unset($data['id']);
        return $this->where($where)->save($data);
    }

    /**
     * 功能说明 通过ID获取数据
     * @author: wang
     * @Date: 2020/3/25 18:17
     * @param $id
     * @return array|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDataById($id)
    {
        $where[] = ['id', '=', $id];
        return $this->where($where)->find();
    }

    /**
     * 功能说明 获取角色列表
     * @author: wang
     * @Date: 2020/3/25 18:16
     * @param $limit
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getManagerList($where, $limit)
    {

        return $this
            ->alias('u')
            ->field('u.id, u.username, u.icon, u.real_name, u.role_id, u.status, u.last_login_time, u.last_login_ip, r.name')
            ->where($where)
            ->leftJoin('role r', 'r.id = u.role_id')
            ->order('u.id desc ')
            ->paginate($limit);
    }

    /**
     * 功能说明 获取全部管理员信息
     * @author: wang
     * @Date: 2020/4/3 11:38
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAllManager()
    {
        return $this->order('id desc')->select();
    }

    /**
     * 功能说明 通过账号获取角色名称
     * @author: wang
     * @Date: 2020/3/31 15:02
     * @param $username
     * @return array|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAdminUserByUserName($username)
    {
        $where[] = ['username', '=', $username];
        return $this->where($where)->find();
    }

    /**
     * 功能说明 通过ID更新状态
     * @Roleor: wang
     * @Date: 2020/3/24 16:45
     * @param $data
     * @return bool
     */
    public function upStatus($data)
    {
        $where[] = ['id', '=', $data['id']];
        $saveData = [
            'status' => $data['status']
        ];
        return $this->where($where)->save($saveData);
    }

    /**
     * 功能说明 通过ID删除数据
     * @Roleor: wang
     * @Date: 2020/3/24 18:18
     * @param $id
     * @return bool
     */
    public function delDataById($id)
    {
        return (new Manager())::destroy($id);
    }

    /**
     * 功能说明 通过更新数据
     * @author: wang
     * @Date: 2020/3/31 15:08
     * @param $id
     * @param $saveData
     * @return bool
     */
    public function upateByID($id, $saveData)
    {
        $where[] = ['id', '=', $id];
        return $this->where($where)->save($saveData);
    }


}